<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Class Portfolio
 * 
 * @property Portfolios $portfolios
 * @property Portfolio_images $portfolio_images
 */
class Portfolio extends CI_Controller {

    private function _build_form($action, $model) {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['action'] = $action;
        $data['model'] = $model;
        $data['options_tahun'] = array();
        $now = date('Y');
        for ($i = 0; $i <= 20; ++$i) {
            $data['options_tahun'][$now - $i] = $now - $i;
        }

        return $this->load->view('portfolio/_form', $data, TRUE);
    }

    private function _check() {
        return (!$this->session->userdata('user'));
    }

    private function _set_data() {
        $this->portfolios->tahun = $this->input->post('tahun');
        $this->portfolios->judul = $this->input->post('judul');
        $this->portfolios->deskripsi = $this->input->post('deskripsi');
        $this->portfolios->pekerja = $this->input->post('pekerja');
    }

    private function _upload_file($name) {
        $config['upload_path'] = './assets/images/portfolio/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);

        return $this->upload->do_upload($name);
    }

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('portfolios');
        $this->load->model('portfolio_images');
    }

    public function index($year = NULL) {
        $data['years'] = $this->portfolios->get_years();

        if (!empty($data['years'])) {
            if ($year == NULL) {
                $year = max($data['years']);
            } else if ($year > max($data['years'])) {
                $year = max($data['years']);
                redirect("portfolio/index/{$year}");
            } else if ($year < min($data['years'])) {
                $year = min($data['years']);
                redirect("portfolio/index/{$year}");
            }
        }

        $data['admin'] = !$this->_check();
        $data['active_year'] = $year;
        $data['title'] = 'portfolio';
        $data['portfolios'] = $this->portfolios->get($year);
        $data['content'] = $this->load->view('portfolio/portfolio', $data, TRUE);
        $this->load->view('front', $data);
    }

    public function add_image($id = NULL) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolios->get_by_id($id);
        if ($model == null) {
            show_404("portoflio/add_image/$id");
            return;
        }

        if (isset($_POST['tambah']) && $this->_upload_file('url')) {
            // Set Data
            $image = new Portfolio_images;
            $image->idportofolio = $id;
            $upload_data = $this->upload->data();
            $image->url = $upload_data['file_name'];

            // Insert
            $image->insert();
        }

        redirect("portfolio/update/$id#portfolio_image");
    }

    public function create() {
        if ($this->_check()) {
            show_404();
            return;
        }

        if (isset($_POST['simpan'])) {
            // Set data
            $this->_set_data();

            // Insert
            if ($this->portfolios->insert()) {
                redirect('portfolio');
            }
        }

        $form = $this->_build_form('portfolio/create', $this->portfolios);
        $content = $this->load->view('portfolio/create', array('form' => $form), TRUE);
        $this->load->view('front', array('content' => $content, 'title' => 'portfolio'));
    }

    public function delete_image($id) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolio_images->get_by_id($id);

        // Hapud dari database
        $this->portfolio_images->delete($id);

        // Hapus file
        @unlink("./assets/images/portfolio/" . $model->url);

        redirect("portfolio/update/{$model->idportofolio}#portfolio_image");
    }

    public function ganti_cover($id = NULL) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolios->get_by_id($id);
        if ($model == null) {
            show_404("portfolio/ganti_cover/$id");
            return;
        }

        if (isset($_POST['ganti']) && $this->_upload_file('gambar_utama')) {
            // Hapus data lama
            @unlink("./assets/images/portfolio/" . $model->gambar_utama);

            // Set data
            $upload_data = $this->upload->data();
            $model->gambar_utama = $upload_data['file_name'];

            // Update
            $model->update();
        }

        redirect("portfolio/update/$id#ganti_cover");
    }

    public function update($id = NULL) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolios->get_by_id($id);
        if ($model == null) {
            show_404("portfolio/update/$id");
            return;
        }

        if (isset($_POST['simpan'])) {
            // Set data
            $this->_set_data();

            // Update
            $this->portfolios->update();
        }

        $form = $this->_build_form("portfolio/update/$id", $model);
        $content = $this->load->view('portfolio/update', array('form' => $form, 'id' => $id), TRUE);
        $this->load->view('front', array('content' => $content, 'title' => 'portfolio'));
    }

    public function update_image_priority($id) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolio_images->get_by_id($id);
        if ($model == null) {
            show_404("portfolio/update_image_priority/$id");
            return;
        }

        if (isset($_POST['ubah'])) {
            $model->prioritas = $_POST['prioritas'];
            $model->update();
        }

        redirect("portfolio/update/{$model->idportofolio}#portfolio_image");
    }

    public function project($id = NULL) {
        $data['model'] = $model = $this->portfolios->get_by_id($id);
        $data['images'] = $model->get_portfolio_images();
        echo $this->load->view('portfolio/_project', $data, TRUE);
    }

    public function delete($id) {
        if ($this->_check()) {
            show_404();
            return;
        }

        $model = $this->portfolios->get_by_id($id);
        if ($model == null) {
            show_404("portfolio/delete/$id");
            return;
        }

        // Hapus cover
        @unlink('./' . Portfolios::IMAGE_PATH . $model->gambar_utama);

        // Hapus semua portofolio_images
        foreach ($model->get_portfolio_images() as $image) {
            @unlink('./' . Portfolios::IMAGE_PATH . $image->url);
        }

        $this->portfolios->delete($id);
        redirect('portfolio');
    }

}